package com.upb.webserver.message;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.enums.MessageStatusEnum;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.pojo.message.ReqMessageReadVO;
import com.upb.webserver.common.tools.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Log4j2
@RestController
@RequestMapping(value = "message")
public class MessageController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private MessageService messageService;


    /**
     * query by page.
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list")
    public BasePageResponse queryMessageByPage(@RequestParam("pageNumber") Integer pageNumber,
                                               @RequestParam("pageSize") Integer pageSize,
                                               @RequestParam(value = "status", required = false) Byte status,
                                               HttpServletRequest request) {
        log.info("start exec method [queryMessageByPage]. pageNumber:{} pageSize:{} status:{}", pageNumber, pageSize, status);

        //get AccountCommon
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        //query message
        BasePageResponse response = messageService.queryMessageByPage(accountCommon.getPlatform(), accountCommon.getId(), status, pageNumber, pageSize);
        log.info("success exec method [queryMessageByPage]. result:{}", JsonUtils.objToString(response));
        return response;
    }


    /**
     * @param param
     * @param result
     * @return
     */
    @PatchMapping(value = "/read")
    public BaseResponse updateStatusToRead(@RequestBody @Valid ReqMessageReadVO param, BindingResult result) {
        log.info("start exec method [updateStatusToRead]. param:{}", JsonUtils.objToString(param));
        messageService.updateStatus(param.getIds(), MessageStatusEnum.HAVE_READ.getId());
        log.info("success exec method [updateStatusToRead]");
        return BaseResponse.initSuccess();
    }

    /**
     * @param request
     * @return
     */
    @GetMapping(value = "/unread/count")
    public BaseResponse countUnReadMessage(HttpServletRequest request) {
        log.info("start exec method [countUnReadMessage].");

        //get AccountCommon
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        //query message
        long count = messageService.countUnReadMessage(accountCommon.getPlatform(), accountCommon.getId());
        BaseResponse response = BaseResponse.initSuccess();
        response.setData(count);
        log.info("success exec method [countUnReadMessage]. response:{}", JsonUtils.objToString(response));
        return response;
    }

}
